import { SelectionModel } from '@angular/cdk/collections';
import { Injectable } from '@angular/core';
import { Pageable } from 'sparrow-permission-lib';
import { NzTableGetdata } from './nz-table-getdata';

export class NzTableCommonService {
  listOfCurrentPageData: readonly any[] = [];
  selectListSelection = new SelectionModel<any>(true);
  indeterminate = false; // use to control the all check style
  allChecked = false;
  total:number =0
  pageable!: Pageable
  constructor(
    private pagedDataService: NzTableGetdata,
    private pageable1: Pageable
  ) {
    this.pageable = this.pageable1
    this.pagedDataService
      .getPagedData({
        page: this.pageable.page! - 1,
        size: this.pageable.size,
        sort: this.pageable.sort,
      })
      .subscribe((res) => {
        this.listOfCurrentPageData = res.content;
        this.total = res.totalElements
      });
  }

  onCurrentPageDataChange(listOfCurrentPageData: readonly any[]): void {
    // this.pagedDataService
    //   .getPagedData({
    //     page: this.pageable.page! - 1,
    //     size: this.pageable.size,
    //     sort: this.pageable.sort,
    //   })
    //   .subscribe((res) => {
    //     this.listOfCurrentPageData = res.content;
    //     this.total = res.totalElements
    //   });
    this.listOfCurrentPageData = listOfCurrentPageData
  }
  onPageIndexChange(page: number) {
    this.pageable.page = page;
    this.pagedDataService
      .getPagedData({
        page: this.pageable.page! - 1,
        size: this.pageable.size,
        sort: this.pageable.sort,
      })
      .subscribe((res) => {
        this.listOfCurrentPageData = res.content;
        this.total = res.totalElements
      });
    this.selectListSelection.clear();
  }

  onPageSizeChange(size: number) {
    this.pageable.size = size;
    this.pagedDataService.getPagedData({
      page: this.pageable.page! - 1,
      size: this.pageable.size,
      sort: this.pageable.sort,
    }).subscribe((res) => {
      this.listOfCurrentPageData = res.content;
        this.total = res.totalElements
    });
    this.selectListSelection.clear();
  }
}
